##################################################
## Replication materials for 
## "Competent legislators or mere pawns? 
## Experimental evidence of attitudes toward gender quota politicians"
## by Carolyn Barnett, Alexandra Blackman, and Marwa Shalaby

## 3. Appendix: Summary Statistics
## This file creates Appendix Table A.1

## NOTE: Run code in file "01_setup.R" first to load packages and data

# Table A.1: Survey Summary Statistics --------

## Original Survey Sample ---------

# NOTE: use "m_summary" dataset with info from all 1800 respondents,
# not only the 927 who participated in the quota experiment

### Prep data for table --------

# All continuous variables
mor_cont <- m_summary %>%
  
  # Select and rename variables 
  dplyr::select(
    `Female` = female,
    `Age` = age,
    `In labor force` = in_laborforce,
    `Unemployment rate` = unemployed,
    `No formal education` = edu_no_formal,
    `Elementary or preparatory` = edu_elem_prep,
    `Secondary` = edu_second,
    `Post-secondary` = edu_above_second,
    `Married or widowed` = married_widowed,
    `Voted in 2021 parliamentary election` = voted_parl21,
    `Urban` = urban
  ) %>%
  
  # Find the mean, st. dev., min, and max for each variable 
  summarise_each(funs(N = sum(!is.na(.)), Mean = mean(., na.rm = T), St.Dev = sd(., na.rm = T), 
                      Min = min(., na.rm = T), Max = max(., na.rm = T))) %>%
  
  # Move summary stats to columns
  gather(key, value, everything()) %>% 
  separate(key, into = c("variable", "stat"), sep = "_") %>%
  spread(stat, value) %>%
  dplyr::rename(Variable = variable) %>%
  
  # Set order of summary statistics 
  dplyr::select(Variable, N, Mean, St.Dev, Min, Max) %>%
  
  
  # Set order of variables
  mutate(Variable = factor(Variable, levels = c("Female", 
                                                "Age", 
                                                "In labor force",
                                                "Unemployment rate", 
                                                "No formal education",
                                                "Elementary or preparatory", 
                                                "Secondary", 
                                                "Post-secondary",
                                                "Married or widowed",
                                                "Voted in 2021 parliamentary election",
                                                "Urban"))) %>%
  
  # Sort
  arrange(Variable) %>%
  
  # Round all numeric variables to one decimal point
  mutate_each(funs(round(., 3)), -Variable)

mor_cont

## Arab Barometer Wave 7 Morocco -------------

### Clean data --------

# Q1001: Age
table(ab7_mor$Q1001)
ab7_mor$age <- ifelse(ab7_mor$Q1001=="Refused to answer", NA, as.numeric(as.character(ab7_mor$Q1001)))
table(ab7_mor$age)


# Q1002: Gender of respondent 
table(ab7_mor$Q1002)
ab7_mor$female <- ifelse(ab7_mor$Q1002=="Female",1, 0)
table(ab7_mor$female)

# Q1005: Employment = 40 percent
table(ab7_mor$Q1005)
ab7_mor$employed <- ifelse(ab7_mor$Q1005=="Employed" | 
                             ab7_mor$Q1005=="Self-Employed",1, 0)
table(ab7_mor$employed)

# Those in labor force (employed and unemployed) == 54.5 percent
ab7_mor$in_laborforce <-  ifelse(ab7_mor$employed==1 | 
                                   ab7_mor$Q1005=="Unemployed", 1,0)
table(ab7_mor$in_laborforce)

# The unemployment rate (unemployed as percentage in labor force) == 30.5 percent
ab7_mor$unemployed <- ifelse(ab7_mor$Q1005=="Employed" | 
                               ab7_mor$Q1005=="Self-Employed",0, 
                              ifelse(ab7_mor$Q1005=="Unemployed",1, NA))

table(ab7_mor$unemployed)

# Q1003: Education
table(ab7_mor$Q1003) # 16.1 percent have no formal
ab7_mor$edu_no_formal <- ifelse(ab7_mor$Q1003=="No formal education",1, 0)
ab7_mor$edu_elem_prep <- ifelse(ab7_mor$Q1003=="Elementary" | ab7_mor$Q1003=="Preparatory/Basic",1, 0)
ab7_mor$edu_second <- ifelse(ab7_mor$Q1003=="Secondary",1, 0)
ab7_mor$edu_above_second <- ifelse(ab7_mor$Q1003=="Technical degree" | 
                                     ab7_mor$Q1003=="BA" | ab7_mor$Q1003=="MA and above",1, 0)

#Urban/Rural
table(ab7_mor$Q13) # 64.5 percent urban
ab7_mor$urban <- ifelse(ab7_mor$Q13=="Urban",1, 0)
table(ab7_mor$urban)

# Vote in last parliamentary election
table(ab7_mor$Q301A) #41.7 percent voted
ab7_mor$voted_parl21 <- ifelse(ab7_mor$Q301A=="Yes",1, 0)
table(ab7_mor$voted_parl21)

# Relationship status
table(ab7_mor$Q1010) # 53.8 percent married or widowed
ab7_mor$married_widowed <- ifelse(ab7_mor$Q1010=="Married" | ab7_mor$Q1010=="Widowed",1, 0)
table(ab7_mor$married_widowed)

### Prep data for table --------

# All continuous variables
mor_ab_cont <- ab7_mor %>%
  
  # Select and rename variables 
  dplyr::select(
    `Female` = female,
    `Age` = age,
    `In labor force` = in_laborforce,
    `Unemployment rate` = unemployed,
    `No formal education` = edu_no_formal,
    `Elementary or preparatory` = edu_elem_prep,
    `Secondary` = edu_second,
    `Post-secondary` = edu_above_second,
    `Married or widowed` = married_widowed,
    `Voted in 2021 parliamentary election` = voted_parl21,
    `Urban` = urban
  ) %>%
  
  # Find the mean, st. dev., min, and max for each variable 
  summarise_each(funs(N = sum(!is.na(.)), Mean = mean(., na.rm = T), St.Dev = sd(., na.rm = T), 
                      Min = min(., na.rm = T), Max = max(., na.rm = T))) %>%
  
  # Move summary stats to columns
  gather(key, value, everything()) %>% 
  separate(key, into = c("variable", "stat"), sep = "_") %>%
  spread(stat, value) %>%
  dplyr::rename(Variable = variable) %>%
  
  # Set order of summary statistics 
  dplyr::select(Variable, N, Mean, St.Dev, Min, Max) %>%
  
  
  # Set order of variables
  mutate(Variable = factor(Variable, levels = c("Female", 
                                                "Age", 
                                                "In labor force",
                                                "Unemployment rate", 
                                                "No formal education",
                                                "Elementary or preparatory", 
                                                "Secondary", 
                                                "Post-secondary",
                                                "Married or widowed",
                                                "Voted in 2021 parliamentary election",
                                                "Urban"))) %>%
  
  # Sort
  arrange(Variable) %>%
  
  # Round all numeric variables to one decimal point
  mutate_each(funs(round(., 3)), -Variable)

mor_ab_cont

## Afrobarometer Wave 9 Morocco -----------

### Clean data ---------------

# Q1: Age
table(af9_mor$Q1)
af9_mor$age <- ifelse(af9_mor$Q1=="Refused", NA, as.numeric(as.character(af9_mor$Q1)))
table(af9_mor$age)


# Q100: Gender of respondent 
table(af9_mor$Q100)
af9_mor$female <- ifelse(af9_mor$Q100=="Woman",1, 0)
table(af9_mor$female)

# Q93A: Employment
table(af9_mor$Q93A)
af9_mor$employed <- ifelse(af9_mor$Q93A=="Yes, full time" | 
                             af9_mor$Q93A=="Yes, part time",1, 0)
table(af9_mor$employed)

# Those in labor force (employed and unemployed) == 63.25 percent
af9_mor$in_laborforce <-  ifelse(af9_mor$employed==1 | 
                                   af9_mor$Q93A=="No (looking)", 1,0)
table(af9_mor$in_laborforce)

# The unemployment rate (unemployed as percentage in labor force) == 16.3 percent
af9_mor$unemployed <- ifelse(af9_mor$Q93A=="Yes, full time" | 
                               af9_mor$Q93A=="Yes, part time",0, 
                              ifelse(af9_mor$Q93A=="No (looking)",1, NA))

table(af9_mor$unemployed)

# Q94: Education
table(af9_mor$Q94) # 15 percent have no formal
af9_mor$edu_no_formal <- ifelse(af9_mor$Q94=="No formal schooling" | 
                                  af9_mor$Q94=="Informal schooling only (including Koranic schooling)",1, 0)
af9_mor$edu_elem_prep <- ifelse(af9_mor$Q94=="Some primary schooling" | 
                                  af9_mor$Q94=="Primary school completed" |
                                  af9_mor$Q94=="Intermediate school or Some secondary school / high school",1, 0)
af9_mor$edu_second <- ifelse(af9_mor$Q94=="Secondary school / high school completed",1, 0)
af9_mor$edu_above_second <- ifelse(af9_mor$Q94=="Post-secondary qualifications, other than university e.g. a diploma or degree from a polytechnic or college" | 
                                     af9_mor$Q94=="Some university" | 
                                     af9_mor$Q94=="University completed" |
                                     af9_mor$Q94=="Post-graduate",1, 0)

#Urban/Rural
table(af9_mor$URBRUR) # 64.6% urban
af9_mor$urban <- ifelse(af9_mor$URBRUR=="Urban",1, 0)
table(af9_mor$urban)

# Vote in last parliamentary election
table(af9_mor$Q13) # 47.5 percent voted
af9_mor$voted_parl21 <- ifelse(af9_mor$Q13=="I voted in the election",1, 0)
table(af9_mor$voted_parl21)

# Relationship status - NOT IN DATA

### Prep data for table ----------------

# All continuous variables
mor_af_cont <- af9_mor %>%
  
  # Select and rename variables 
  dplyr::select(
    `Female` = female,
    `Age` = age,
    `In labor force` = in_laborforce,
    `Unemployment rate` = unemployed,
    `No formal education` = edu_no_formal,
    `Elementary or preparatory` = edu_elem_prep,
    `Secondary` = edu_second,
    `Post-secondary` = edu_above_second,
    #`Married or widowed` = married_widowed,
    `Voted in 2021 parliamentary election` = voted_parl21,
    `Urban` = urban
  ) %>%
  
  # Find the mean, st. dev., min, and max for each variable 
  summarise_each(funs(N = sum(!is.na(.)), Mean = mean(., na.rm = T), St.Dev = sd(., na.rm = T), 
                      Min = min(., na.rm = T), Max = max(., na.rm = T))) %>%
  
  # Move summary stats to columns
  gather(key, value, everything()) %>% 
  separate(key, into = c("variable", "stat"), sep = "_") %>%
  spread(stat, value) %>%
  dplyr::rename(Variable = variable) %>%
  
  # Set order of summary statistics 
  dplyr::select(Variable, N, Mean, St.Dev, Min, Max) %>%
  
  
  # Set order of variables
  mutate(Variable = factor(Variable, levels = c("Female", 
                                                "Age", 
                                                "In labor force",
                                                "Unemployment rate", 
                                                "No formal education",
                                                "Elementary or preparatory", 
                                                "Secondary", 
                                                "Post-secondary",
                                                #"Married or widowed",
                                                "Voted in 2021 parliamentary election",
                                                "Urban"))) %>%
  
  # Sort
  arrange(Variable) %>%
  
  # Round all numeric variables to one decimal point
  mutate_each(funs(round(., 3)), -Variable)

mor_af_cont


## World Values Survey Wave 7 Morocco ----------

### Clean data ------------

# Q262: Age
table(wvs7_mor$Q262)
wvs7_mor$age <- wvs7_mor$Q262
table(wvs7_mor$age)


# Q260: Gender of respondent 
table(wvs7_mor$Q260)
wvs7_mor$female <- ifelse(wvs7_mor$Q260==2,1, 0)
wvs7_mor$Q260 <- factor(wvs7_mor$Q260,levels = c("1","2"),labels = c("Male","Female"))
table(wvs7_mor$female)

# Q279: Employment
table(wvs7_mor$Q279)
wvs7_mor$employed <- ifelse(wvs7_mor$Q279==1 | 
                              wvs7_mor$Q279==2 | 
                              wvs7_mor$Q279==3, 1, 0)
table(wvs7_mor$employed)

# Those in labor force (employed and unemployed) == 67.75 percent
wvs7_mor$in_laborforce <-  ifelse(wvs7_mor$Q279 %in% c(1,2,3,7), 1,0)
table(wvs7_mor$in_laborforce)

# The unemployment rate (unemployed as percentage in labor force) == 16.9 percent
wvs7_mor$unemployed <- ifelse(wvs7_mor$Q279 %in% c(1,2,3), 0, 
                               ifelse(wvs7_mor$Q279==7, 1, NA))

table(wvs7_mor$unemployed)

# Q275: Education
table(wvs7_mor$Q275) # 29.8 percent have no formal
wvs7_mor$edu_no_formal <- ifelse(wvs7_mor$Q275==0,1, 0)
wvs7_mor$edu_elem_prep <- ifelse(wvs7_mor$Q275==1 | wvs7_mor$Q275==2,1, 0)
wvs7_mor$edu_second <- ifelse(wvs7_mor$Q275==3,1, 0)
wvs7_mor$edu_above_second <- ifelse(wvs7_mor$Q275==4 | wvs7_mor$Q275==5 | 
                                      wvs7_mor$Q275==6 | wvs7_mor$Q275==7 |
                                      wvs7_mor$Q275==8,1, 0)
table(wvs7_mor$edu_above_second)

#Urban/Rural
table(wvs7_mor$H_URBRURAL) # 68.333% urban
wvs7_mor$urban <- ifelse(wvs7_mor$H_URBRURAL==1,1, 0)
table(wvs7_mor$urban)

# Vote in last parliamentary election 
### NOT ASKED

# Q273: Relationship status
table(wvs7_mor$Q273) # 57.2 percent married or widowed
wvs7_mor$married_widowed <- ifelse(wvs7_mor$Q273==1 | wvs7_mor$Q273==5,1, 0)
table(wvs7_mor$married_widowed)

### Prep data for table ---------

# All continuous variables
mor_wvs_cont <- wvs7_mor %>%
  
  # Select and rename variables 
  dplyr::select(
    `Female` = female,
    `Age` = age,
    `In labor force` = in_laborforce,
    `Unemployment rate` = unemployed,
    `No formal education` = edu_no_formal,
    `Elementary or preparatory` = edu_elem_prep,
    `Secondary` = edu_second,
    `Post-secondary` = edu_above_second,
    `Married or widowed` = married_widowed,
    #`Voted in 2021 parliamentary election` = voted_parl21,
    `Urban` = urban
  ) %>%
  
  # Find the mean, st. dev., min, and max for each variable 
  summarise_each(funs(N = sum(!is.na(.)), Mean = mean(., na.rm = T), St.Dev = sd(., na.rm = T), 
                      Min = min(., na.rm = T), Max = max(., na.rm = T))) %>%
  
  # Move summary stats to columns
  gather(key, value, everything()) %>% 
  separate(key, into = c("variable", "stat"), sep = "_") %>%
  spread(stat, value) %>%
  dplyr::rename(Variable = variable) %>%
  
  # Set order of summary statistics 
  dplyr::select(Variable, N, Mean, St.Dev, Min, Max) %>%
  
  
  # Set order of variables
  mutate(Variable = factor(Variable, levels = c("Female", 
                                                "Age", 
                                                "In labor force",
                                                "Unemployment rate", 
                                                "No formal education",
                                                "Elementary or preparatory", 
                                                "Secondary", 
                                                "Post-secondary",
                                                "Married or widowed",
                                                #"Voted in 2021 parliamentary election",
                                                "Urban"))) %>%
  
  # Sort
  arrange(Variable) %>%
  
  # Round all numeric variables to one decimal point
  mutate_each(funs(round(., 3)), -Variable)

mor_wvs_cont


## Combine tables for Appendix -------------

# Join our survey, Arab Barometer (Rd 7), AF survey (Rd. 9) and WVS (Rd. 7)
sum <- rbind(mor_cont, 
             mor_ab_cont, 
             mor_af_cont,
             mor_wvs_cont) %>%
  mutate(N = round(N, 0))

# Appendix Table A.1
print(xtable(sum, type = "latex", 
             caption="Survey Summary Statistics", 
             label = "summarystats_comb", digits=c(0,0,0,3,3,0,0)), 
      table.placement = getOption("xtable.table.placement", "H"),
      hline.after = getOption("xtable.hline.after", c(-1,0,11, nrow(sum))),
      include.rownames=FALSE,
      caption.placement="top",
      size="scriptsize")





